package com.jake.dynamicproxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class CglibProxyFactory {

    public static Object createProxy(final Object delegate) {
        return Enhancer.create(delegate.getClass(), new MethodInterceptor() {
            @Override
            public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws InvocationTargetException, IllegalAccessException {
                before();
                Object result = method.invoke(delegate, args);
                after();
                return result;
            }

            private void before() {
                System.out.println("inner cglib proxy: before method");
            }

            private void after() {
                System.out.println("inner cglib proxy: after method");
            }
        });
    }
}
